// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'
import Client from '@api/sparepartshospital/api/SpareAssetTypeApi'

import ComClass from '@api/ComQuery'
import ListClass from '@api/sparepartshospital/model/SpareAssetTypeListQueryReq'
import OneClass from '@api/sparepartshospital/model/SpareAssetTypeReq'
import AddClass from '@api/sparepartshospital/model/SpareAssetTypeOperateReq'
import BaseClass from '@api/sparepartshospital/model/SpareAssetTypeDto'
import ChildClass from '@api/sparepartshospital/model/SpareDto'

const EditClass = AddClass
const DelClass = OneClass

const CLIENT = CORE.grpcCLIENT(Client)
const COM = [ComClass, 'comQuery']
const CHILDREN = [ChildClass, 'content', 'children']

// ============================== 导出远程接口 ============================== //

export default {
  // —————————————————————————————— 列表 —————————————————————————————— //

  async getList(param, TIP = '关联设备列表查询') {
    const DTO = CORE.grpcDTO(ListClass, param, COM)
    const resp = await CORE.grpc(TIP, CLIENT, 'querySpareAssetTypeList', DTO)
    return [resp.comQuery.totalCount, resp.content || []]
  },

  // —————————————————————————————— 单项 —————————————————————————————— //

  async getOne({ id }, TIP = '查询关联设备详情') {
    const DTO = CORE.grpcDTO(OneClass, { ids: [id] })
    const resp = await CORE.grpc(TIP, CLIENT, 'querySpareAssetTypeDetail', DTO)
    return resp.content || {}
  },
  transOne(obj) {
    obj.children = obj.content || []
    return obj
  },

  // —————————————————————————————— 增 —————————————————————————————— //

  async add(one, TIP = '新增关联设备') {
    const DTO = new AddClass()
    const baseDTO = CORE.grpcDTO(BaseClass, one, CHILDREN)
    DTO.setContent(baseDTO)

    await CORE.grpc(TIP, CLIENT, 'insertSpareAssetType', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 改 —————————————————————————————— //

  async edit(one, TIP = '修改关联设备') {
    const DTO = new EditClass()
    const baseDTO = CORE.grpcDTO(BaseClass, one, CHILDREN)
    DTO.setContent(baseDTO)

    await CORE.grpc(TIP, CLIENT, 'updateSpareAssetType', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 删 —————————————————————————————— //

  async del({ id }, TIP = '删除关联设备') {
    const DTO = CORE.grpcDTO(DelClass, { ids: [id] })
    await CORE.grpc(TIP, CLIENT, 'deleteSpareAssetType', DTO)
    return CORE.grpcOK(TIP)
  },
}
